const Base = require('./base.js');
const fs = require('fs');
const rp = require('request-promise');

module.exports = class extends Base {
  async avatarAction() {
    const brandFile = this.file('file');
    if (think.isEmpty(brandFile)) {
      return this.fail('保存失败');
    }
    const that = this;
    const filename = '/static/avatar/' + think.uuid(32) + '.jpg';
    const is = fs.createReadStream(brandFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);

    return that.success({
      name: 'pic',
      url: think.config('baseUrl') + filename,
    });
  }

  async cvAction() {
    const cvFile = this.file('file');
    const userToken = this.post('token');
    const isSave = this.post('save');

    if (think.isEmpty(cvFile)) {
      return this.fail('上传失败');
    }
    if (think.isEmpty(userToken)) {
      return this.fail('上传失败');
    }

    const tokenSerivce = think.service('token', 'api');
    const userId = await tokenSerivce.getUserId(userToken);

    if (!userId) {
      return this.fail('上传失败');
    }

    try {
      const options = {
        method: 'POST',
        uri: 'https://api.xiaoxizn.com/v1/peipei/resumes?handle_image=1',
        headers: {
          'content-type': 'application/json',
          id: '3ed9df30-b8e9-11ed-9a3c-bd45ade9b6eb',
          secret: '31646037-95e3-4e27-a140-24a18d3b9f02',
        },
        body: {
          resume_base: `${Buffer.from(fs.readFileSync(cvFile.path)).toString(
            'base64'
          )}`,
          file_name: cvFile.name,
          predicted_result: 1,
          update_mode: 'keep_latest',
        },
        json: true,
      };
      const res = await rp(options);

      if (res.errorcode === 0) {
        let info = {};
        console.log(isSave);
        if (isSave == '1') {
          await this.model('user_list').where({ id: userId }).update({
            cv_id: res.cv_id,
          });
          // console.log(123123);
          // const options2 = {
          //   method: 'GET',
          //   uri: `https://api.xiaoxizn.com/v1/peipei/resumes/${res.cv_id}`,
          //   headers: {
          //     'content-type': 'application/json',
          //     id: '3ed9df30-b8e9-11ed-9a3c-bd45ade9b6eb',
          //     secret: '31646037-95e3-4e27-a140-24a18d3b9f02',
          //   },
          // };
          // console.log(options2);
          // const detail = await rp(options2);
          // console.log(detail, 7777);
          // if (JSON.parse(detail).resume) {
          //   info.parsing_result = JSON.parse(detail).resume.parsing_result;
          //   info.predicted_result = JSON.parse(detail).resume.predicted_result;
          // }
          // console.log(JSON.parse(detail));
        }
        // await this.model('user_list').where({ id: userId }).update({
        //   cv_id: res.cv_id,
        // });
        // const newUserInfo = await this.model('user_list')
        //   .where({ id: userId })
        //   .find();
        return this.success({
          cv_id: res.cv_id,
          cv_name: cvFile.name,
          info: info,
        });
      } else {
        return this.fail('上传失败');
      }
    } catch (e) {
      return null;
    }

    // const that = this;
    // const filename = '/static/banner/' + think.uuid(32) + '.jpg';
    // const is = fs.createReadStream(cvFile.path);
    // const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    // is.pipe(os);

    // return that.success({
    //   name: 'pic',
    //   url: think.config('baseUrl') + filename,
    // });
  }

  async newsThumbAction() {
    const brandFile = this.file('file');
    if (think.isEmpty(brandFile)) {
      return this.fail('保存失败');
    }
    const that = this;
    const filename = '/static/news/' + think.uuid(32) + '.jpg';
    const is = fs.createReadStream(brandFile.path);
    const os = fs.createWriteStream(think.ROOT_PATH + '/www' + filename);
    is.pipe(os);

    return that.success({
      name: 'pic',
      url: think.config('baseUrl') + filename,
    });
  }

  async checkVipAction() {
    await this.model('user').where('DATEDIFF(curdate(),end_time)>0').update({
      is_vip: 0,
    });

    await this.model('figure')
      .where('DATEDIFF(curdate(),dated_time)>0')
      .update({
        is_vip: 0,
      });

    return this.success();
  }
};
